import java.util.concurrent.CountDownLatch;

/*
    CountDownLatch test
 */
public class CountDownDemo {
    private static final  int COUNT = 6; //倒计数,用于countdownlatch计数
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        for (int i = 1; i < 7; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t 正在取餐");
                countDownLatch.countDown();
            }, CountEnum.forEach_CountEnum(i).getRetMessage()).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + ": 所有人都取完餐，窗口关闭");
    }
}

/**
 * 枚举类像一个数据库，避免了数据库连接池异常，可以提高系统性能
 */
enum CountEnum{
    ONE(1,"a"),TWO(2,"b"),Three(3,"c"),
    FOUR(4,"d"),FIVE(5,"e"),SIX(6,"f");

    private Integer retCode;
    private String retMessage;

    CountEnum(Integer retCode, String retMessage){
        this.retCode = retCode;
        this.retMessage = retMessage;
    }

    public Integer getRetCode() {
        return retCode;
    }

    public void setRetCode(Integer retCode) {
        this.retCode = retCode;
    }

    public String getRetMessage() {
        return retMessage;
    }

    public void setRetMessage(String retMessage) {
        this.retMessage = retMessage;
    }

    public static CountEnum forEach_CountEnum(int index){
        CountEnum[] arr = CountEnum.values();
        for (CountEnum ele : arr) {
            if (index == ele.getRetCode()){
                return ele;
            }
        }
        return null;
    }
}

